package br.com.siscom.controller;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.siscom.conexao.InterfacePool;
import br.com.siscom.conexao.Pool;
import br.com.siscom.model.command.InterfaceCommand;
import br.com.siscom.model.helper.SiscomHelper;

/**
 * Servlet implementation class for Servlet: SiscomController
 *
 */
 public class SiscomController extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
   private InterfacePool pool = new Pool();
   private SiscomHelper siscomHelper = new SiscomHelper(pool);
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public SiscomController() {
		super();
	}
	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		this.pool = (InterfacePool)getServletContext().getAttribute("pool");
	}
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processarRequisicao(request, response);
	}  	
	
	private void processarRequisicao(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException 
	{
		siscomHelper.setRequest(request);
		InterfaceCommand comando = siscomHelper.getCommand();
		String pagina = comando.execute(request, response);
		request.getRequestDispatcher(pagina).include(request, response);
	}

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processarRequisicao(request, response);
	}   	  	    
}